<?php
require 'BaseController.php';

class Admin_SlideController extends Admin_BaseController {
	
	private $_slideModel;
	
		public function init(){
		parent::init();
		$auth = Zend_Auth::getInstance();
		$this->_identity = $auth->getIdentity();
		$this->view->identity = $this->_identity;
		$this->_slideModel = new Model_Slide();
		}
		
	public function indexAction(){
		$this->view->headScript()->appendFile($this->view->baseUrl().'/public/plugins/ckfinder/ckfinder.js');
		//khai báo session 
		$session = new Zend_Session_Namespace();
		$this->view->message  = $session->message;
		$session->__unset('message');
		
		//lấy  các banner
		$this->view->arrImages= $this->_slideModel->getAllImages();
		
		$request = $this->getRequest();
		//lấy banner hiện tại nếu có
		$id = $request->getParam('id');
		if(is_numeric($id)){
			$image = $this->_slideModel->getImageByID($id);
			if($image != null) $this->view->image = $image;
		}
		//kiểm tra post
		if($request->isPost()){
			$src = $request->getParam('src');
			$caption = $request->getParam('caption');
			$link = $request->getParam('link');
			$priority = $request->getParam('priority');
			if(!isset($image)){
				if($this->_slideModel->createImage($src, $caption, $link, $priority)){
					$session->message = "<span class='success'>Thêm ảnh thành công</span>";
				}else{
					$session->message = "<span class='error'>Thêm ảnh không thành công</span>";
				}
			}else{
				if($this->_slideModel->updateImage($id, $src, $caption, $link, $priority)){
					$session->message = "<span class='success'>Sửa ảnh thành công</span>";
					$this->_redirector->gotoRoute(array('id'=>''), "admin_slide");
				}else{
					$session->message = "<span class='error'>Sửa banner không thành công</span>";
				}
			}
			$this->_redirector->gotoRoute(array('id'=>''), "admin_slide");
		}
		
	}
	public function deleteAction(){
		$session = new Zend_Session_Namespace();
		if($this->getRequest()->isPost()){
			$id = $this->getRequest()->getParam('ids');
		}else{
			$id = $this->getRequest()->getParam('id');
		}
		if($this->_slideModel->deleteImages($id)){
			$session->message = '<span class="success">Xóa ảnh thành công</span>';
		}else{
			$session->message = '<span class="error">Xóa ảnh không thành công</span>';
		}
		$this->_redirector->gotoRoute(array(), 'admin_slide');
	}
}